<template>
	<view>
		<cu-custom bgColor="bg-blue" :isBack="true">
			<block slot="content">注册</block>
		</cu-custom>
		<view class="cu-form-group">
			<view class="title">用户名</view>
			<input placeholder="请输入用户名" type="text" v-model="userInfoForm.username" />
		</view>
		<view class="cu-form-group">
			<view class="title">密码</view>
			<input placeholder="请输入密码" type="password" v-model="userInfoForm.password" />
		</view>
		<view class="cu-form-group">
			<view class="title">确认密码</view>
			<input placeholder="请确认密码" type="password" v-model="userInfoForm.ensurePassword" />
		</view>
		<view class="cu-form-group solid-bottom">
			<view class="title">昵称</view>
			<input placeholder="请输入昵称" type="text" v-model="userInfoForm.nickname" />
		</view>
		<view class="cu-form-group solid-bottom">
			<view class="title">邮箱</view>
			<input placeholder="请输入邮箱" type="text" v-model="userInfoForm.email" />
		</view>
		<view class="padding flex flex-direction"><button @click="handleRegister" class="cu-btn bg-blue lg">注册</button>
		</view>
	</view>
</template>

<script>
	import JSEncrypt from '@/lib/jsencrypt.min.js';
	import {
		getPublicKeyForRegisterApi,
		userRegisterApi
	} from '../../api/http.js';
	export default {
		data() {
			return {
				userInfoForm: {
					username: '',
					password: '',
					ensurePassword: '',
					nickname: '',
					uuid: '',
					email: '',
				},
				publicKey: ''
			};
		},
		onLoad() {
			getPublicKeyForRegisterApi().then(res => {
				this.userInfoForm.uuid = res.header.uuid;
				this.publicKey = res.header.sign;
			});
		},
		methods: {
			showToastWithMsg(msg) {
				uni.showToast({
					title: msg,
					duration: 1000,
					icon: 'none'
				});
			},
			handleRegister() {
				if (this.userInfoForm.username === '') {
					this.showToastWithMsg('请输入用户名');
					return;
				}
				if (this.userInfoForm.password === '') {
					this.showToastWithMsg('请输入密码');
					return;
				}
				if (this.userInfoForm.nickname === '') {
					this.showToastWithMsg('请输入昵称');
					return;
				}
				if (this.userInfoForm.password != this.userInfoForm.ensurePassword) {
					this.showToastWithMsg('两次密码不一致');
					return;
				}
				const reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/;
				if (this.userInfoForm.email === '') {
					this.showToastWithMsg('请输入邮箱');
					return;
				}
				if (!reg.test(this.userInfoForm.email)) {
					this.showToastWithMsg('邮箱格式不正确');
					return;
				}
				let formCopy = Object.assign({}, this.userInfoForm);
				formCopy.password = this.encryptPassword(formCopy.password);
				userRegisterApi(formCopy).then(res => {
					uni.navigateTo({
						url: '../login/login',
						success() {
							uni.showToast({
								title: '注册成功，可以使用账号进行登录了'
							});
						}
					});
				});
			},
			encryptPassword(pass) {
				let jse = new JSEncrypt();
				jse.setPublicKey(this.publicKey);
				return jse.encrypt(pass);
			}
		}
	};
</script>

<style lang="scss" scoped>
	page {
		background-color: #ffffff;
	}
</style>
